﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using Business.Models;
using System.Data;

namespace Business.DAO
{
    public class QuanTriDAO
    {
        private ISSContext db = new ISSContext();
        public List<QuanTri> GetAll()
        {
            var quantri = db.QuanTri.Include(g => g.NhanVien);
            return quantri.ToList();
        }

        public QuanTri Get(int id)
        {
            QuanTri quantri = db.QuanTri.Find(id);
            return quantri;
        }

        public QuanTri GetQuanTri_Super(int idTruongHoc)   
        {
            try
            {
                QuanTri quantri = db.QuanTri.Where(m => m.NhanVien.IdTruongHoc == idTruongHoc && m.NhanVien.NguoiTao.HasValue == false).First();
                return quantri;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public QuanTri Login(String Name, String Password)
        {
            try
            {
                QuanTri a = db.QuanTri.Where(m => m.NhanVien.TenNhanVien == Name && m.NhanVien.MatKhau == Password).First();
                return a;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public int Create(QuanTri quantri)
        {
            try
            {
                db.QuanTri.Add(quantri);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Edit(QuanTri quantri)
        {
            try
            {
                db.Entry(quantri).State = EntityState.Modified;
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int Delete(int id)
        {
            try
            {
                QuanTri quantri = db.QuanTri.Find(id);
                db.QuanTri.Remove(quantri);
                return db.SaveChanges();
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    }
}